<?php

namespace ApiBundle\Controller;

use Symfony\Component\BrowserKit\Response;

class ConfigController extends ApiController
{
    /**
     * 数据包
     *
     * 返回软件基础数据
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function dataPackAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $client = $this->getPlatform();

        $data = array();

        return $this->response('成功得到数据', 0, $data);
    }

    /**
     * 配置
     *
     * 通过修改配置项影响app的执行
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function appConfigAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $client = $this->getPlatform();

        $config['is_suspend'] = $this->getParameter($client . '_is_suspend', 0);  // 是否中断运行
        $config['suspend_jump_url'] = $this->getParameter($client . '_suspend_jump_url'); // 中断跳转url

        return $this->response('', 0, $config);
    }

    /**
     * 版本更新
     *
     * @return array|bool|\Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    public function versionUpdateAction()
    {
        $r = $this->inlet($this->getParameter('return_data'), false);
        if($r !== true){
            return $r;
        }

        $client = $this->getPlatform();

        $apk_download_url = $this->getParameter($client . '_apk_download_url');
        $apk_version = $this->getParameter($client . '_apk_version');
        $apk_last_force_update_version = $this->getParameter($client . '_apk_last_force_update_version');
        $message = array(
            'title' => $this->getParameter($client . '_versionUpdate_title', '版本更新'),
            'items' => $this->getParameter($client . '_versionUpdate_content', array())
        );

        $force = 0; //是否强制更新，默认否

        if(!empty($apk_last_force_update_version) && $this->getAppVersion() < $this->getAppVersion($apk_last_force_update_version)){
            $force = 1;
        }

        $data['updated'] = array(
            $data['updated'] = array(
                'force' => $force,
                'version' => $apk_version,
                'download' => $apk_download_url,
                'message' => $message
            )
        );

        return $this->response('成功得到数据', 0, $data);
    }

    /**
     * 下载
     *
     * @param $client
     * @return Response
     */
    public function downloadAction($client)
    {
        $os = $this->get('unicorn.systems')->getOS();

        switch ($os){
            case 'weixin':
                $url = $this->getParameter($client . '_wxdownload_url');
                break;
            case 'iphone':
                $url = $this->getParameter($client . '_ios_apk_download_url');
                break;
            default:
                $url = $this->getParameter($client . '_apk_download_url');

//                //第三方应用商城 识别手机型号前往不同的应用商城
//                $agent = strtolower($this->get('request')->server->get('HTTP_USER_AGENT'));

//                if(strpos($agent, 'xiaomi') || strpos($agent, 'redmi')){
//                }
//
//                if(strpos($agent, 'huawei')){
//                }
//
//                if(strpos($agent, 'oppo')){
//                }
//
//                if(strpos($agent, 'vivo')){
//                }
//
//                if(strpos($agent, 'mz')){
//                }
//
//                if(strpos($agent, 'lenovo')){
//                }
        }

        $this->goUrl($url);

        return new Response();
    }

    public function getParameter($name, $default = '')
    {
        return  $this->container->hasParameter($name) ? parent::getParameter($name) : $default;
    }
}